package com.shizhanzhe.zxbapp.vip;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.Notice;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.bean.VIPDiscount;
import com.shizhanzhe.zxbapp.customtask.PublishActivity;
import com.shizhanzhe.zxbapp.home.SourceMaterialActivity;
import com.shizhanzhe.zxbapp.pay.PayVIP;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.task.FastTaskActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.ErrorCallback;
import com.shizhanzhe.zxbapp.widget.MakeMoneyDialog;
import com.shizhanzhe.zxbapp.widget.MyListView;
import com.shizhanzhe.zxbapp.widget.SignDialog;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.logging.Level;

/**
 * Created by zz9527 on 2018/11/12.
 */
public class VipFragment extends Fragment implements View.OnClickListener {
    private MyListView notice;
    private ImmersionBar mImmersionBar;
    private TextView name;
    LoadService loadService;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //第一步：获取布局View
        View rootView=View.inflate(getActivity(),R.layout.activity_vip,  null);
        //第二步：注册布局View
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // 重新加载逻辑
                getData();
            }
        });
        //第三步：返回LoadSir生成的LoadLayout
        return loadService.getLoadLayout();
    }



    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initImmersionBar();
        getVipDiscount();
        initView(view);
        getData();
    }

    private void getVipDiscount() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.VIPDISCOUNT(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                Gson gson = new Gson();
                try {
                    VIPDiscount.DataBean bean = gson.fromJson(json, VIPDiscount.class).getData().get(0);
                    if ("".equals(bean.getImg())) {
                    } else {
                        if (UserBean.level == 0) {
                            final MakeMoneyDialog makeDIalog = new MakeMoneyDialog(getContext());
                            makeDIalog.setListener(new MakeMoneyDialog.MakeMoneyListener() {
                                @Override
                                public void checktrue() {
                                    startActivityForResult(new Intent(getContext(), PayVIP.class), 1);
                                    makeDIalog.dismiss();

                                }
                            });
                            makeDIalog.show();
                            makeDIalog.setImg(Path.img(bean.getImg()));
                        }
                    }
                } catch (Exception e) {

                }
            }

        });
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!"".equals(UserBean.nickname)) {
            name.setText(UserBean.nickname);
        }
        if (!hidden && mImmersionBar != null) {
            ImmersionBar.with(this)
                    .statusBarDarkFont(true, 0.2f)
                    .init();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    private RelativeLayout vip_user;
    private ImageView buy;

    private void initView(View view) {
        view.findViewById(R.id.backarea).setOnClickListener(this);
        buy = view.findViewById(R.id.iv_vipbuy);
        buy.setOnClickListener(this);
        view.findViewById(R.id.vip_gaoe).setOnClickListener(this);
        view.findViewById(R.id.vip_kuaisu).setOnClickListener(this);
        view.findViewById(R.id.vip_more).setOnClickListener(this);
        view.findViewById(R.id.vip_yongjin).setOnClickListener(this);
        view.findViewById(R.id.vip_sucai).setOnClickListener(this);
        view.findViewById(R.id.vip_circle).setOnClickListener(this);
        view.findViewById(R.id.gz).setOnClickListener(this);
        view.findViewById(R.id.xs).setOnClickListener(this);
        ImageView iv = view.findViewById(R.id.img);
        name = view.findViewById(R.id.name);
        TextView up = view.findViewById(R.id.up);
        if (UserBean.level == 3) {
            up.setText("最高等级");
        } else {
            up.setOnClickListener(this);
        }

        vip_user = view.findViewById(R.id.vip_user);
        notice = view.findViewById(R.id.notice);
        notice.setFocusable(false);
        if (UserBean.level > 0) {
            vip_user.setVisibility(View.VISIBLE);
            buy.setVisibility(View.GONE);
            if (UserBean.level == 1) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip1));
            } else if (UserBean.level == 2) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip2));
            } else if (UserBean.level == 3) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip3));
            }
        } else {
            vip_user.setVisibility(View.GONE);
            buy.setVisibility(View.VISIBLE);
        }
        Glide.with(this).load(Path.img(UserBean.headimg)).apply(new RequestOptions().error(R.mipmap.logo).circleCrop()).into(iv);

        view.findViewById(R.id.publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.level > 0) {
                    startActivity(new Intent(getActivity(), PublishActivity.class));
                } else {
                    final VipDIalog vipDIalog = new VipDIalog(getContext());
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            startActivity(new Intent(getContext(), PayVIP.class));
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();

                        }
                    });
                    vipDIalog.show();
                }
            }
        });
    }

    protected void getData() {
        OkHttpDownloadJsonUtil.downloadJson(getActivity(), Path.NOTICE(1), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        final List<Notice.DataBean> list = gson.fromJson(json, Notice.class).getData();
                        notice.setAdapter(new NoticeAdapter(getActivity(), list));
                        notice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                                intent.putExtra("title", list.get(i).getTitle());
                                intent.putExtra("id", list.get(i).getId());
                                intent.putExtra("type", 3);
                                startActivity(intent);
                            }
                        });
                        loadService.showSuccess();
                    } catch (Exception e) {
                        loadService.showCallback(ErrorCallback.class);
                    }
                } else {
                    loadService.showCallback(ErrorCallback.class);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!"".equals(UserBean.nickname)) {
            name.setText(UserBean.nickname);
        }
        if (UserBean.level > 0) {
            vip_user.setVisibility(View.VISIBLE);
            buy.setVisibility(View.GONE);
            if (UserBean.level == 1) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip1));
            } else if (UserBean.level == 2) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip2));
            } else if (UserBean.level == 3) {
                vip_user.setBackground(getResources().getDrawable(R.mipmap.bg_vip3));
            }
        } else {
            vip_user.setVisibility(View.GONE);
            buy.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backarea:
                getActivity().finish();
                break;
            case R.id.iv_vipbuy:
                if (UserBean.level > 0) {
                    Toast.makeText(getActivity(), "您已是VIP用户", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(getActivity(), PayVIP.class));
                }
                break;
            case R.id.vip_kuaisu:

                Intent intent = new Intent(getActivity(), FastTaskActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);

                break;
            case R.id.vip_gaoe:
//                Intent intent2 = new Intent(getActivity(), FastTaskActivity.class);
//                intent2.putExtra("type", 2);
//                startActivity(intent2);
                startActivity(new Intent(getContext(), VipHighAcitivity.class));
                break;
            case R.id.vip_circle:
                if (UserBean.gao > 0) {
                    startActivity(new Intent(getContext(),HairCircleActivity.class));
                } else {
                    final SignDialog dialog=new SignDialog(getContext());
                    dialog.setListener(new SignDialog.SignListener() {
                        @Override
                        public void checktrue() {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    dialog.showGao("尚未提现","完成首次提现后即可进入");
                    dialog.showImg(R.mipmap.dialog_tx);
                }
                break;
            case R.id.vip_more:
                Intent intent3 = new Intent(getActivity(), VipMoreActivity.class);
                intent3.putExtra("type", 3);
                startActivity(intent3);
                break;
            case R.id.vip_yongjin:
                startActivity(new Intent(getActivity(), PDDActivity.class));
                break;
            case R.id.vip_sucai:
                if (UserBean.level > 0) {
                    startActivity(new Intent(getActivity(), SourceMaterialActivity.class).putExtra("type", 1));

                } else {
                    final VipDIalog vipDIalog = new VipDIalog(getContext());
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            startActivity(new Intent(getContext(), PayVIP.class));
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();

                        }
                    });
                    vipDIalog.show();
                }
                break;
            case R.id.gz:
                if (UserBean.level > 0) {
                    startActivity(new Intent(getActivity(), NoticeActivity.class).putExtra("type", 2));
                } else {
                    final VipDIalog vipDIalog = new VipDIalog(getContext());
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            startActivity(new Intent(getContext(), PayVIP.class));
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();

                        }
                    });
                    vipDIalog.show();
                }
                break;

            case R.id.xs:
                if (UserBean.level > 0) {
                    startActivity(new Intent(getActivity(), NoticeActivity.class).putExtra("type", 1));
                } else {
                    final VipDIalog vipDIalog = new VipDIalog(getContext());
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            startActivity(new Intent(getContext(), PayVIP.class));
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();

                        }
                    });
                    vipDIalog.show();
                }
                break;
            case R.id.up:
                final VipDIalog vipDIalog = new VipDIalog(getContext());
                vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                    @Override
                    public void chooseTrue() {
                        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.VIPUPDATE(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                            @Override
                            public void onsendJson(String json) {
                                try {
                                    JSONObject object = new JSONObject(json);
                                    String info = object.optString("info");
                                    Toast.makeText(getContext(), info, Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {


                                }
                            }
                        });
                        vipDIalog.dismiss();
                    }

                    @Override
                    public void chooseFalse() {
                        vipDIalog.dismiss();

                    }
                });
                vipDIalog.show();
                if (UserBean.level == 1) {
                    vipDIalog.upVipzhangmeng();
                } else if (UserBean.level == 2) {
                    vipDIalog.upVipmengzhu();
                }


                break;
            default:
                break;
        }
    }
}
